import request from "../request";
import type { getSearchListParams, setListRes } from "@/types/global";

// 订单列表
interface searchListItem {
  autoConfirmDay: number;
  billContent: string;
  billHeader: string;
  billReceiverEmail: string;
  billReceiverPhone: string;
  billType: number;
  commentTime: string;
  confirmStatus: number;
  couponAmount: number;
  couponId: number;
  createTime: string;
  deleteStatus: number;
  deliveryCompany: string;
  deliverySn: string;
  deliveryTime: string;
  discountAmount: number;
  freightAmount: number;
  growth: number;
  id: number;
  integration: number;
  integrationAmount: number;
  memberId: number;
  memberUsername: string;
  modifyTime: string;
  note: string;
  orderSn: string;
  orderType: number;
  payAmount: number;
  payType: number;
  paymentTime: string;
  promotionAmount: number;
  promotionInfo: string;
  receiveTime: string;
  receiverCity: string;
  receiverDetailAddress: string;
  receiverName: string;
  receiverPhone: string;
  receiverPostCode: string;
  receiverProvince: string;
  receiverRegion: string;
  sourceType: number;
  status: number;
  totalAmount: number;
  useIntegration: number;
}
// 查询订单返回值
interface searchListRes {
  list: Array<searchListItem>;
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

// 退货申请查询参数
interface returnGoodsParams {
  createTime?: string;
  handleMan?: string;
  handleTime?: string;
  id?: number;
  pageNum: number;
  pageSize: number;
  receiverKeyword?: string;
  status?: number;
}
// 退货申请listItem
interface returnGoodsListItem {
  id: number;
  orderId: null;
  companyAddressId: null;
  productId: null;
  orderSn: null;
  createTime: string | number;
  memberUsername: string;
  returnAmount: null;
  returnName: string;
  returnPhone: null;
  status: number;
  handleTime: string;
  productPic: null;
  productName: null;
  productBrand: null;
  productAttr: null;
  productCount: 1;
  productPrice: null;
  productRealPrice: number;
  reason: null;
  description: null;
  proofPics: null;
  handleNote: null;
  handleMan: null;
  receiveMan: null;
  receiveTime: null;
  receiveNote: null;
}

// 退货申请返回值
interface returnGoodsRes {
  list: Array<returnGoodsListItem>;
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

// 修改订单参数
interface setListParams {
  id: number;
  orderSetting: string | any;
}

// 备注订单参数
interface upDateNoteParams {
  id: number;
  note: string;
  status: number;
}

interface upDateNoteRes {
  code?: number;
  data?: any;
  message?: string;
}

/*
 *  获取指定订单设置
 */
interface IRequestOrderSetting {
  id: number;
  flashOrderOvertime: number;
  normalOrderOvertime: number;
  confirmOvertime: number;
  finishOvertime: number;
  commentOvertime: number;
}

/**
 * 分页查询退货申请 列表 - params
 */
interface IReturnApplyListParams {
  createTime?: string; // 申请时间
  handleMan?: string; // 处理人员
  handleTime?: string; // 处理时间
  id?: number | undefined; // 服务单号
  pageNum?: number;
  pageSize?: number;
  receiverKeyword?: string; // 收货人姓名/号码
  status?: 0 | 1 | 2 | 3 | undefined; // 申请状态：0->待处理；1->退货中；2->已完成；3->已拒绝
}
/**
 * 查询订单 - model
 */
interface IOrderListModel {
  pageNum: number;
  pageSize: number;
  totalPage: number;
  total: number;
  list: {}[];
}
/**
 * 添加退货原因 - params
 */
interface IReturnReasonCreateParams {
  id?: number;
  name?: string;
  sort: number;
  status: number;
  createTime?: string;
}
/**
 * 根据id 获取退货申请详情
 */
interface IReturnApplyByIdModel {
  id: number;
  orderId: number;
  companyAddressId: number;
  productId: number;
  orderSn: string;
  createTime: string;
  memberUsername: string;
  returnAmount: number;
  returnName: string;
  returnPhone: string;
  status: number;
  handleTime: string;
  productPic: string;
  productName: string;
  productBrand: string;
  productAttr: string;
  productCount: number;
  productPrice: number;
  productRealPrice: number;
  reason: string;
  description: string;
  proofPics: string;
  handleNote: string;
  handleMan: string;
  receiveMan: string;
  receiveTime: string;
  receiveNote?: any;
  companyAddress: any;
}

/**
 * 获取所有收货地址 - model
 */
interface ICompanyAddressListModel {
  id: number;
  addressName: string;
  sendStatus: number;
  receiveStatus: number;
  name: string;
  phone: string;
  province: string;
  city?: string;
  region: string;
  detailAddress: string;
}

// 格式
interface Data<T> {
  code: number;
  message: string;
  data: T;
}

// 查询订单
const searchListAPI = (data?: getSearchListParams): Promise<searchListRes> =>
  request.get("/order/list", { params: data });

// 修改订单设置
const setListAPI = (data?: setListParams): any => {
  request.post(`/orderSetting/update/${data?.id}`, data);
};

// 分页查询退货申请处理
const returnGoodsAPI = (data: returnGoodsParams): Promise<returnGoodsRes> =>
  request.get("/returnApply/list", { data });

// 备注订单
const upDateNoteAPI = (data?: upDateNoteParams): Promise<upDateNoteRes> =>
  request.post("order/update/note", data);

/*
 * 获取指定订单设置
 */
const APIOrderSetting = (id: number): Promise<Data<IRequestOrderSetting>> =>
  request.get(`orderSetting/${id}`);
/**
 * 修改指定订单设置
 */
const APIOrderSettingUpdate = (
  params: IRequestOrderSetting
): Promise<Data<IRequestOrderSetting>> =>
  request.post(`/orderSetting/update/${params.id}`, params);

/**
 * 分页查询退货申请 列表
 */
const APIReturnApplyList = (
  params: IReturnApplyListParams
): Promise<Data<IOrderListModel>> =>
  request.get("/returnApply/list", { params });
/**
 * 根据id 批量删除退货申请
 */
const APIReturnApplyDelete = (params: { ids: string }): Promise<Data<number>> =>
  request.post("/returnApply/delete", undefined, { params });

/**
 * 根据id 获取退货申请详情
 */
const APIReturnApplyById = (id: number): Promise<Data<IReturnApplyByIdModel>> =>
  request.get(`/returnApply/${id}`);

/**
 * 获取所有收货地址
 */
const APICompanyAddressList = (): any => request.get(`/companyAddress/list`);

export {
  APIReturnApplyById,
  searchListAPI,
  setListAPI,
  returnGoodsAPI,
  upDateNoteAPI,
  APIOrderSetting,
  APIOrderSettingUpdate,
  APIReturnApplyList,
  APIReturnApplyDelete,
  APICompanyAddressList,
};
